package com.chagee.channel.server.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.alibaba.ttl.threadpool.TtlExecutors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author chang.jiawei
 * @Date 2024/7/1
 **/
@Configuration
public class ThreadPoolConfig {

	@Bean("cosSyncExecutor")
	public ThreadPoolExecutor qimaiSyncExecutor() {
		ThreadFactory threadFactory = new ThreadFactoryBuilder().setNamePrefix("cos-sync-thread-").build();
		return new ThreadPoolExecutor(5, 20, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(200), threadFactory);
	}
    @Bean("ttlExecutor")
    public ExecutorService ttlExecutor() {
        // 使用TTL线程池，保证threadLocal传递
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNamePrefix("ttl-executor-thread-").build();
        return TtlExecutors
            .getTtlExecutorService(new ThreadPoolExecutor(6, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), threadFactory, new ThreadPoolExecutor.CallerRunsPolicy()));
    }


}
